package com.wfk.h5.service;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.image.common.RestUploadFileInfo;
import com.wfk.alipay.AliSmsUtil;
import com.wfk.constant.PathConstant;
import com.wfk.entity.Account;
import com.wfk.entity.Customer;
import com.wfk.exception.ServiceException;
import com.wfk.jwt.JWT;
import com.wfk.service.IAccountService;
import com.wfk.service.IBaseInfoService;
import com.wfk.service.IBasicsSmsInfoService;
import com.wfk.service.ICustomerService;
import com.wfk.utils.Login;
import com.wfk.utils.MD5Util;
import com.wfk.utils.ResponseData;
import com.wfk.utils.ValidateUtil;

import cn.hutool.core.codec.Base64;

@Service
public class FrontService {
	@Autowired
	private IBaseInfoService baseInfoService;
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private IAccountService accountService;
	@Autowired
	private IBasicsSmsInfoService basicsSmsInfoService;
	
	/**
	 * 扫描端登陆
	 * @param phone 手机号
	 * @param password 密码
	 * @return
	 */
	public ResponseData macLogin(String phone,String password) {
		if(!ValidateUtil.empty(phone)) {
			return ResponseData.baseServiceError("请输入账号");
		}else if(!ValidateUtil.empty(password)) {
			return ResponseData.baseServiceError("请输入密码");
		}
		String pwd = MD5Util.md5Hex(password);
		QueryWrapper<Customer> query = new QueryWrapper<Customer>();
		query.eq("phone",phone);
		Customer cus = this.customerService.getOne(query);
		if(ValidateUtil.objEmpty(cus)) {
			return ResponseData.baseServiceError("手机号未注册");
		}else if(!ValidateUtil.equalTo(pwd, cus.getPassword())) {
			return ResponseData.baseServiceError("账号或密码错误");
		}else if(ValidateUtil.isEquals(cus.getStatus(), -1)) {
			return ResponseData.baseServiceError("账号已被冻结");
		}else if(!ValidateUtil.isEquals(cus.getIsopen(),1)) {
			return ResponseData.baseError("请先购买流量机");
		}
		Login login  =new Login();
		login.setId(cus.getId());
		String token = JWT.sign(login,TimeUnit.DAYS.toMillis(90)) ;//加密1天
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("cid", cus.getId());
		map.put("level", cus.getLevelid());
		map.put("nikname",cus.getNikname());
		map.put("token",token);
		return ResponseData.ok().putDataValue("result", map);
	}
	
	/**
	 * 用户登陆
	 * @param phone 手机号
	 * @param password 密码
	 * @return
	 */
	public ResponseData login(String phone,String password) {
		if(!ValidateUtil.empty(phone)) {
			return ResponseData.baseServiceError("请输入账号");
		}else if(!ValidateUtil.empty(password)) {
			return ResponseData.baseServiceError("请输入密码");
		}
		String pwd = MD5Util.md5Hex(password);
		QueryWrapper<Customer> query = new QueryWrapper<Customer>();
		query.eq("phone",phone);
		Customer cus = this.customerService.getOne(query);
		if(ValidateUtil.objEmpty(cus)) {
			return ResponseData.baseServiceError("手机号未注册");
		}else if(!ValidateUtil.equalTo(pwd, cus.getPassword())) {
			return ResponseData.baseServiceError("账号或密码错误");
		}else if(ValidateUtil.isEquals(cus.getStatus(), -1)) {
			return ResponseData.baseServiceError("账号已被冻结");
		}
		Login login  =new Login();
		login.setId(cus.getId());
		String token = JWT.sign(login,TimeUnit.DAYS.toMillis(90)) ;//加密1天
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("cid", cus.getId());
		map.put("level", cus.getLevelid());
		map.put("nikname",cus.getNikname());
		map.put("token",token);
		return ResponseData.ok().putDataValue("result", map);
	}

	public static void main(String[] args) {
		Login login  =new Login();
		login.setId(1023);
		String token = JWT.sign(login,TimeUnit.DAYS.toMillis(3)) ;//加密1天
		System.out.println(token);
		Login s = JWT.unsign(token, Login.class);
		System.out.println(s.getId());
	}
	
	/**
	 * app注册
	 * @param phone 注册手机号
	 * @param password 密码
	 * @param realname 名字
	 * @param vehiclenumber 车牌号
	 * @param parentid 推荐人id
	 * @param validcode 验证码
	 * @return
	 */
	public ResponseData handleAppRegister(String phone,String password,Integer parentid,String validcode){
		if(!ValidateUtil.empty(phone)){
			return ResponseData.baseServiceError("请输入正确的手机号");
		}else if(!ValidateUtil.empty(password)){
			return ResponseData.baseServiceError("密码8~16位字母或数字");
		}else if(!ValidateUtil.empty(validcode)){
			return ResponseData.baseServiceError("请输入正确的验证码");
		}
//		ResponseData result=basicsSmsInfoService.validSMS(phone,validcode);
//		if(result.getCode()!=200) {
//			return ResponseData.baseServiceError(result.getMessage());
//		}
		QueryWrapper<Customer> queryWrapper = new QueryWrapper<Customer>();
		queryWrapper.eq("phone", phone);
		Integer count =this.customerService.count(queryWrapper);
		if(count!=null&&count>0) {
			return ResponseData.baseServiceError("手机号已被注册");
		}
		Customer parent = null;
		if(!ValidateUtil.objEmpty(parentid)){
			parent = this.customerService.getById(parentid);
			if(parent==null){
				return ResponseData.baseServiceError("推荐人不存在");
			}
		}
		//保存用户分享二维码
		String appurl=baseInfoService.getByKey("appurl");
		String pwd = MD5Util.md5Hex(password);
		Customer cus = new Customer();
		cus.setPhone(phone);
		cus.setPassword(pwd);
		cus.setNikname(phone);
		cus.setRealname(phone);
		cus.setParentid(parentid);
//		cus.setHeadurl(appurl+"/images/oval.png");
		if(!ValidateUtil.objEmpty(parent)){
			if(ValidateUtil.empty(parent.getGenes())) {
				cus.setGeneration(parent.getGeneration()+1);
				cus.setGenes(parent.getGenes()+parent.getId()+",");
			}else {
				cus.setGeneration(parent.getGeneration()+1);
				cus.setGenes(parent.getId()+",");
			}
		}else if(ValidateUtil.objEmpty(parent)){
			cus.setGeneration(1);
			cus.setGenes("");
		}
		boolean  saveflag = this.customerService.save(cus);
		if(saveflag){
			Account account=new Account();
			account.setCid(cus.getId());
			this.accountService.save(account);
			String url=appurl+"/shareUser?tjphone="+cus.getId();
			RestUploadFileInfo info = com.image.common.Service.uploadQR(PathConstant.appImgUrl,url);
			String qrurl = info.getServiceName() + info.getFilePath() + info.getFileName();
			if(!ValidateUtil.empty(qrurl)){
				throw new ServiceException("推荐二维码无法生成，请稍后"); 
			}
			cus.setQrurl(qrurl);
			this.customerService.updateById(cus);
			return ResponseData.ok();
		}
		return ResponseData.baseServiceError("你注册失败好像网络出现问题了");
	}
	
	/**
	 * 发送注册验证码
	 * @param phone
	 * @return
	 */
	public ResponseData sendRegister(String phone) {
		QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("phone", phone);
		Integer count=customerService.count(queryWrapper);
		if(count!=null&&count>0) {
			return ResponseData.baseServiceError("手机号已被注册");
		}
//		ResponseData result=basicsSmsInfoService.sendSMS(phone, AliSmsUtil.SIGN_NAME,AliSmsUtil.TEMPLATE_CODE_REGISTER);
//		if(result.getCode()!=200) {
//			return ResponseData.baseServiceError(result.getMessage());
//		}
		return ResponseData.ok("发送成功，请注意查收");
	}
	
	/**
	 * 忘记密码短信
	 * @param phone
	 * @return
	 */
	public ResponseData sendPassword(String phone){
		QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("phone", phone);
		Integer count=customerService.count(queryWrapper);
		if(count==null||count<1){
			return ResponseData.baseServiceError("手机号未注册");
		}
//		ResponseData result=basicsSmsInfoService.sendSMS(phone, AliSmsUtil.SIGN_NAME,AliSmsUtil.TEMPLATE_CODE_UPDATE);
//		if(result.getCode()!=200) {
//			return ResponseData.baseServiceError(result.getMessage());
//		}
		return ResponseData.ok("发送成功，请注意查收");
	}
}
